package net.jltec.view.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.jltec.config.AppConfig;
import net.jltec.utils.AlertDialogUtil;
import net.jltec.utils.CallBackMethod;
import net.jltec.utils.ProcessMethod;
import net.jltec.utils.WebUtils;
import net.jltec.view.R;
import net.jltec.view.base.JltecBaseUI;
import net.jltec.view.bean.Task;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
@SuppressWarnings({"unchecked","rawtypes"})
public class JltecTaskDetailUI extends JltecBaseUI implements OnClickListener{
	private TextView titleTextView;
	private TextView  taskSpNote;
	private EditText taskNote;
	private Task task;
	private ListView taskDetailListView; 
	private RadioGroup group ; //点选按钮组
	private Button submitBtn;
	private Button cancelBtn;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 声明使用自定义标题
		setContentView(R.layout.main_task_detail);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main_title);// 自定义布局赋值
		initParams();
	}
	
	private void initParams(){
		
		titleTextView = (TextView) findViewById(R.id.main_title_id);
		
		
		taskSpNote= (TextView) findViewById(R.id.task_sp_note);
		taskNote = (EditText) findViewById(R.id.task_note_edit);
		
		task = (Task) getIntent().getSerializableExtra("model");
		if (task != null) {
			titleTextView.setText("流水号:" + task.wiid);
			taskSpNote.setText(task.note);
			loadTaskDetail(task);
		}

		
		submitBtn = (Button) findViewById(R.id.task_detail_sumbit);
		submitBtn.setOnClickListener(this);
		cancelBtn = (Button) findViewById(R.id.task_detail_cancel);
		cancelBtn.setOnClickListener(this);
		
	 
	}
	
	private void initSPYJ(Map rs){
		List vars = (List) rs.get("vars");
		if(vars!=null){
			group =(RadioGroup) findViewById(R.id.task_detail_radio_group);
			group.removeAllViews();

			boolean first = true;
			// 添加单选按钮
			for (int i = 0; i <vars.size(); i++) {
				Map var = (Map) vars.get(i);
				RadioButton radio = new RadioButton(this);
				radio.setText(convert(var.get("name"),String.class));
				radio.setId(convert(var.get("value"),Integer.class));
				radio.setTextColor(Color.BLACK);
				if(first){
					radio.setChecked(true);
					first=false;
				}
				
				group.addView(radio);
			}
			
			group.setTag(rs);
		}
		
		
		
	}
	
	
	
	private void loadTaskDetail(final Task task){
		asynMethod(new ProcessMethod() {
			@Override
			public Object execute() {
				Map<String,String> params = createOAuthUserMap();
				params.put("winid", task.winid+"");
				String result =  WebUtils.doGet(AppConfig.GET_FLOW_BY_WINID,params);
				Map rs = JSON.parseObject(result, Map.class);
				return rs;
			}
		}, new CallBackMethod() {
			@Override
			public void execute(Object result) {
				Map params = (Map) result;
				initSPYJ(params);
			}
		});
	}

	@Override
	public void onClick(View v) {
		if(v==submitBtn){
			doSumbit();
		}else if(v==cancelBtn){
			this.finish();
		}
	}
	
	
	private void doSumbit(){
		
		AlertDialogUtil.createSubmitDialog(this,   new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				final int valueId = group.getCheckedRadioButtonId();
				final Map rs = (Map) group.getTag();
				asynMethod(new ProcessMethod() {
					@Override
					public Object execute() {
						Map params = createOAuthUserMap();
						params.put("winid", rs.get("winid"));
						params.put("varid", rs.get("varid"));
						params.put("wiid",task.wiid);
						params.put("value", valueId);
						params.put("note", taskNote.getText().toString());
						
						String json = WebUtils.doPost(AppConfig.GOTO_NEXT_FLOW, params);
						
						return json;
					}
				}, new CallBackMethod() {
					@Override
					public void execute(Object result) {
						Intent data = new Intent();
						HashMap params = JSON.parseObject(result.toString(), HashMap.class);
						data.putExtra("params", params);
						setResult(JltecTaskUI.OPT_SP, data);
						finish();
					}
				});
				dialog.dismiss();
			}
		},new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
	 
	}
}
